package com.airlines.crewassignment.domain.util;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;

import com.airlines.common.ListSortUtil;
import com.airlines.common.SpringContextUtil;
import com.airlines.crewassignment.domain.Crew;
import com.airlines.crewassignment.domain.CrewPositionAssignment;
import com.airlines.crewassignment.domain.DateTimeRange;
import com.airlines.crewassignment.domain.FlightInfo;
import com.airlines.crewassignment.domain.LinkLine;
import com.airlines.crewassignment.entity.ValueForDate;
import com.airlines.crewassignment.service.linkline.TaskLoopFlightFactService;

/**
 * @author xxxx
 * @date 2018年4月16日 
 * @description
 */
 
public class FlightDurationUtil {

//	private static LinkLineLoopService linkLineLoopService;
//	private static LinkLineLoopService getLinkLineLoopService(){
//		if(linkLineLoopService == null){
//			linkLineLoopService = (LinkLineLoopService)SpringContextUtil.getBean(LinkLineLoopService.class);
//		}
//		return linkLineLoopService;
//	}
	
	private static TaskLoopFlightFactService taskLoopFlightFactService;
	private static TaskLoopFlightFactService getTaskLoopFlightFactService(){
		if(taskLoopFlightFactService == null){
			taskLoopFlightFactService = (TaskLoopFlightFactService)SpringContextUtil.getBean(TaskLoopFlightFactService.class);
		}
		return taskLoopFlightFactService;
	}
	
	/**
	 * 获取一个飞行员任意月的最大飞行时间
	 * 任意月，一般是排班日期开始当月，如果排班日期跨月，则包括下一月，以及未来任务环所在月的段
	 * 
	 * @return
	 */
	public static int getMaxFlightDurationEveryMonth(List<CrewPositionAssignment> list,Crew crew){
		int flightDurationLastMonth = crew.getCrewFlightExperience().getFlightDurationLastMonth();
		List<ValueForDate> valueForDateListAll =  getMaxFlightDurationEveryDateRange(list,crew,flightDurationLastMonth,"month");
		//按月分组合计
		List<ValueForDate> groupByMonth = ValueForDateUtil.groupByMonth(valueForDateListAll);
		return ValueForDateUtil.getMaxValue(groupByMonth);
	}
	
	
	/**
	 * 获取一个飞行员任意年的最大飞行时间
	 * 任意月，一般是排班日期开始当年，如果排班日期跨年，则包括下一年，以及未来任务环所在年的段
	 * @param list
	 * @param crew
	 * @return
	 */
	public static int getMaxFlightDurationEveryYear(List<CrewPositionAssignment> list,Crew crew){
		int flightDurationLastYear = crew.getCrewFlightExperience().getFlightDurationLastYear();
		List<ValueForDate> valueForDateListAll =  getMaxFlightDurationEveryDateRange(list,crew,flightDurationLastYear,"year");
		//按年分组合计
		List<ValueForDate> groupByYear = ValueForDateUtil.groupByYear(valueForDateListAll);
		return ValueForDateUtil.getMaxValue(groupByYear);
	}
	
	
	/**
	 * 将每天的值组装到每日实体类中
	 * @param list
	 * @param crew
	 * @param flightDurationLast
	 * @param yearOrMonth
	 * @return
	 */
	private static List<ValueForDate> getMaxFlightDurationEveryDateRange(List<CrewPositionAssignment> list,Crew crew,int flightDurationLast,String yearOrMonth){
		List<ValueForDate> valueForDateList = new ArrayList<>();
		//以前的飞行时间，算作排班第一天的
		valueForDateList.add(new ValueForDate(crew.getCrewFlightExperience().getBeforeFlightDate(),flightDurationLast));
		
		if(CollectionUtils.isEmpty(list)){
			return valueForDateList;
		}
		
		//当前排班任务的
		List<LinkLine> linkLineList = new ArrayList<>();
		for(CrewPositionAssignment cpa : list){
			LinkLine linkLine  = cpa.getPosition().getLinkLine();
			linkLineList.add(linkLine);
			for(FlightInfo f : linkLine.getFlightInfoList()){
				DateTimeRange dr = new DateTimeRange(f.getStd(),f.getSta());
				int flightDuration = dr.getDuration();
				ValueForDate plan = new ValueForDate(f.getFlightDate(),flightDuration);
				valueForDateList.add(plan);
			}
		}
		
		//未来可能的的，算作排班最后一天的
		ListSortUtil.sort(linkLineList, true, "ta");
		
//		LinkLineLoopService linkLineLoopService  = getLinkLineLoopService();
		LinkLine linkLineEnd  = linkLineList.get(linkLineList.size()-1);
		DateTime flightDateTimeEnd = new DateTime(linkLineEnd.getFlightDate());
		DateTime end = null;
		if(StringUtils.equals("month", yearOrMonth)){
			end = flightDateTimeEnd.dayOfMonth().withMaximumValue();
		}else if(StringUtils.equals("year", yearOrMonth)){
			end = flightDateTimeEnd.dayOfYear().withMaximumValue();
		}
		
		//int flightDurationFuture  = linkLineLoopService.getRemainFlyDurationOfLoopPlan(linkLineEnd.getLinkLineLoopFact(), end.toDate());
		TaskLoopFlightFactService taskLoopFlightFactService = getTaskLoopFlightFactService();
		int flightDurationFuture =taskLoopFlightFactService.getRemainFlyDurationOfLoopPlan(linkLineEnd, end.toDate());
		ValueForDate future = new ValueForDate(linkLineEnd.getFlightDate(),flightDurationFuture);
		valueForDateList.add(future);
		
		return valueForDateList;
	}
	
	
	/**
	 * 计算某一天某个任务环的飞行时间,单位为分钟
	 * @param flightInfoList 是某个任务环某一天的所有连线
	 * @return
	 */
	public static int  getFlightDuration(List<FlightInfo> flightInfoList){
		int flightDurationSum = 0;
		if(CollectionUtils.isEmpty(flightInfoList)){
			return flightDurationSum;
		}
		for(FlightInfo f: flightInfoList ){
			DateTimeRange dr = new DateTimeRange(f.getStd(),f.getSta());
			int flightDuration = dr.getDuration();
			flightDurationSum += flightDuration;
		}
		return flightDurationSum;
		
	}
}
